﻿using System;
using System.IO;
namespace FileUtility.Log
{
    public class Log
    {
         private static Log instance;
         private static object lockObj = new object();
         public static Log getInstance()
         {

             if (instance == null)
             {
                 lock (lockObj)
                 {
                     if (instance == null)
                     {
                         instance = new Log();
                     }
                 }
             }

             return instance;
         }

        //public void WriteToLog(String content, string FatherLogPath)
        //{
        //    try
        //    {
        //        if (!Directory.Exists(FatherLogPath))//判断路径是否存在，若不存在则创建。
        //        {
        //            Directory.CreateDirectory(FatherLogPath);//创建新路径
        //        }
        //       using( StreamWriter sw = new StreamWriter(FatherLogPath +"/"+ DateTime.Now.ToString("yyyy-MM-dd") + ".txt",true,System.Text.Encoding.Default))
        //       {
        //        sw.WriteLine(DateTime.Now.ToString("------------------yyyy-mm-dd hh:mm:ss-------------------------"));
        //        sw.WriteLine(DateTime.Now.ToString(content));
        //        sw.WriteLine("------------------------------------------------------------------");
        //        sw.Close();
        //       }
        //    }
        //    catch { }
        //}

        public void WriteToLog(String content, string FatherLogPath)
        {
            try
            {
                //string logPath = FatherLogPath + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                string time = DateTime.Now.ToString("------------------yyyy-MM-dd hh:mm:ss-------------------------") + Environment.NewLine;
                content = time + content + Environment.NewLine;
                if (!Directory.Exists(FatherLogPath))//判断路径是否存在，若不存在则创建。
                {
                    Directory.CreateDirectory(FatherLogPath);//创建新路径
                }
                string path = FatherLogPath + "/" + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                //FileStream fs = new FileStream(path, FileMode.Append);
                using (FileStream fs = new FileStream(path, FileMode.Append, FileAccess.Write))
                {

                    char[] charDataValue = content.ToCharArray();
                    byte[] byDataValue = new byte[charDataValue.Length];
                    System.Text.Encoder ec = System.Text.Encoding.UTF8.GetEncoder();
                    ec.GetBytes(charDataValue, 0, charDataValue.Length, byDataValue, 0, true);
                    // fs.Seek(0, SeekOrigin.Begin);
                    fs.Write(byDataValue, 0, byDataValue.Length);
                }

            }
            catch { }
        }
        //public static void LogMessage(string Meesage,string FatherLogPath)
        //{
        //    if (!System.IO.Directory.Exists(FatherLogPath))
        //    {
        //        System.IO.Directory.CreateDirectory(FatherLogPath);
        //    }
        //    string fileName=System.DateTime.Now.ToString("yyyyMMdd")+".txt";
        //    string filePath=FatherLogPath + "\\" + fileName;
        //    if(!System.IO.File.Exists(filePath))
        //    {
        //        System.IO.File.Create(filePath);
        //    }
        //    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath, true))
        //    {
        //        sw.WriteLine(Meesage);
        //    }
        //}
    }
}
